package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.JwtTokenMetadata
import com.zxy.supplier_system.server.service.AuthorizationService
import jakarta.servlet.http.HttpServletRequest
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("authorization")
class AuthorizationController(
    private val authorizationService: AuthorizationService,
) {

    @PutMapping("logIn", params = ["phoneNumber", "password"])
    fun logInByPhoneNumberAndPassword(
        phoneNumber: String,
        password: String,
        @RequestParam(defaultValue = "APP") client: JwtTokenMetadata.Client = JwtTokenMetadata.Client.APP,
        enterpriseCode: String,
    ): ResponseEntity<String> {
        return this.authorizationService.logInByPhoneNumberAndPassword(phoneNumber, password, client, enterpriseCode)
    }

    @DeleteMapping
    fun logOut(request: HttpServletRequest) {
        return this.authorizationService.logOut(request)
    }

    data class UpdatePasswordRequest(
        val password: String,
        val newPassword: String,
    )

    @PostMapping("updatePassword")
    fun updatePassword(@RequestBody request: UpdatePasswordRequest) {
        this.authorizationService.updatePassword(request)
    }

    @RequestMapping("logIn", params = ["wechatCode"], method = [RequestMethod.PUT, RequestMethod.POST])
    fun logInByWechatOpenId(wechatCode: String, enterpriseCode: String): ResponseEntity<String> {
        return this.authorizationService.logInByWechatCode(wechatCode,enterpriseCode)
    }

    @RequestMapping("logIn", params = ["wechatCode", "phoneNumber", "password"], method = [RequestMethod.PUT, RequestMethod.POST])
    fun logInByWechatOpenIdAndPhoneNumber(
        wechatCode: String,
        phoneNumber: String,
        password: String,
        enterpriseCode: String
    ): ResponseEntity<String> {
        return this.authorizationService.logInByWechatOpenIdAndPhoneNumber(wechatCode, phoneNumber, password,enterpriseCode)
    }

}